package game.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/*
 * 猜拳 判断的输赢条件
 * */
public class CaiquanUtil
{
	public static int  shiTou = 0;
	public static int  jianDao = 1;
	public static int  bu = 2 ;
	/*重写自我介绍
	 */
	public  String toString()
	{
		return "石头:("+shiTou +");剪刀:("+jianDao+");布:("+ bu+")";
	}
	/*用户已经输入
	 * 电脑随机出一个 判断输赢
	 * */
	public Map<String, Integer> wan(BufferedReader br)
	{
		Map<String, Integer> resultMap = new HashMap<String,Integer>();
		/*统计*/
		int totalCount = 0 ;
		int winCount = 0;
		int loseCount = 0;
		int pingCount = 0;
		try
		{
			while(true)
			{
				System.out.println("=不想玩,就输入N="+this.toString());
				/*存储 用户输入的结果 每次读一行*/
				String line = br.readLine();
				/*去掉字符串中的空格*/
				line = line.trim();
				if("n".equalsIgnoreCase(line))
				{
					System.out.println("=退出当前游戏=");
					break;
				}
				try
				{
					int usersShu = Integer.valueOf(line);
					/*电脑随机生成的随机数*/
					Random random = new Random();
					/*电脑输入*/
					int comShu = random.nextInt(3);
					/*判断输赢*/
					int result = this.panduan(usersShu,comShu);
					/*共多少局*/
					totalCount ++ ;
					if (result == 0)
					{
						/*赢了多少局*/
						winCount ++ ;
					}else if(result ==1)
					{
						/*输了多少局*/
						loseCount ++ ;
					}else if(result==2)
					{
						/*平了多少局*/
						pingCount++;
					}
					System.out.println("您出的:("+usersShu+");电脑输出的:("+comShu+")");
					/*显示一下*/
					this.iffo(result, totalCount, winCount, loseCount,pingCount);
				}catch (NumberFormatException e)
				{
					System.out.println("=您输入的有误,请重新输入=");
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		resultMap.put("totalCount", totalCount);
		resultMap.put("winCount", winCount);
		resultMap.put("loseCount", loseCount);
		resultMap.put("pingCount", pingCount);
		return resultMap ;
	}
	private void iffo(int result, int totalCount, int winCount, int loseCount, int pingCount)
	{
		StringBuffer sb = new StringBuffer();
		/*0: 用户赢了  1:用户输了   2:平局*/
		if(result == 0)
		{
			sb.append("恭喜你赢了\r\n");
		}else if(result==1)
		{
			sb.append("不甘心再来一局\r\n");
		}else if(result==2)
		{
			sb.append("平了,在来一把\r\n");
		}
		sb.append("您与电脑的比分("+winCount+":"+loseCount+")");
		System.out.println(sb);
	}
	/*判断输赢的方法
	 * 0 , 1 , 2   =>石头, 剪刀  , 布
	 * 		用户	电脑
	 * 	赢	0	1
	 * 	赢	1	2
	 * 	赢	2	0
	 * */
	private int panduan(int usersShu, int comShu)
	{
		if(usersShu== 0 || usersShu==1 || usersShu ==2)
		{
		}else
		{
			/*当用户输入有误是执行此处*/
			System.out.println("=请按照规定输入=");
			return -1;
		}
		if(usersShu == comShu)
		{
			return 2;
		}else if(usersShu - comShu == -1 || usersShu - comShu == 2)
		{
			return 0;
		}else
		{
			return 1;
		}
	}
}
